package com.knowledgegraph.controller;

import com.knowledgegraph.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 用于测试API接口和日志功能
 */
@Slf4j
@RestController
@RequestMapping("/api/test")
public class TestController {

    /**
     * 测试GET请求
     */
    @GetMapping("/get")
    public Result<Map<String, Object>> testGet(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer age,
            HttpServletRequest request) {
        
        log.info("测试GET请求 - name: {}, age: {}", name, age);
        
        Map<String, Object> data = new HashMap<>();
        data.put("method", "GET");
        data.put("name", name);
        data.put("age", age);
        data.put("timestamp", System.currentTimeMillis());
        
        return Result.success("GET请求测试成功", data);
    }

    /**
     * 测试POST请求
     */
    @PostMapping("/post")
    public Result<Map<String, Object>> testPost(@RequestBody Map<String, Object> requestData) {
        
        log.info("测试POST请求 - 请求数据: {}", requestData);
        
        Map<String, Object> data = new HashMap<>();
        data.put("method", "POST");
        data.put("receivedData", requestData);
        data.put("timestamp", System.currentTimeMillis());
        
        return Result.success("POST请求测试成功", data);
    }

    /**
     * 测试PUT请求
     */
    @PutMapping("/put/{id}")
    public Result<Map<String, Object>> testPut(
            @PathVariable Long id,
            @RequestBody Map<String, Object> requestData) {
        
        log.info("测试PUT请求 - id: {}, 请求数据: {}", id, requestData);
        
        Map<String, Object> data = new HashMap<>();
        data.put("method", "PUT");
        data.put("id", id);
        data.put("receivedData", requestData);
        data.put("timestamp", System.currentTimeMillis());
        
        return Result.success("PUT请求测试成功", data);
    }

    /**
     * 测试DELETE请求
     */
    @DeleteMapping("/delete/{id}")
    public Result<Map<String, Object>> testDelete(@PathVariable Long id) {
        
        log.info("测试DELETE请求 - id: {}", id);
        
        Map<String, Object> data = new HashMap<>();
        data.put("method", "DELETE");
        data.put("id", id);
        data.put("timestamp", System.currentTimeMillis());
        
        return Result.success("DELETE请求测试成功", data);
    }

    /**
     * 测试异常处理
     */
    @GetMapping("/error")
    public Result<Object> testError() {
        log.info("测试异常处理");
        throw new RuntimeException("这是一个测试异常");
    }

    /**
     * 获取请求头信息
     */
    @GetMapping("/headers")
    public Result<Map<String, Object>> getHeaders(HttpServletRequest request) {
        
        Map<String, String> headers = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("headers", headers);
        data.put("remoteAddr", request.getRemoteAddr());
        data.put("requestURI", request.getRequestURI());
        data.put("method", request.getMethod());
        
        log.info("获取请求头信息: {}", data);
        
        return Result.success("获取请求头成功", data);
    }
}